package Thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

class MyBlockingQueue<E>{
   Object[] array;
    int size;
    int head;
    int tail;
    public MyBlockingQueue(int elem) {
        array = new Object[elem];
    }

   synchronized  void put(E values) throws InterruptedException {
       while (size == array.length){
           this.wait();
       }
        array[tail] = values;
        tail++;
        size++;
       this.notify();
       if(tail == array.length){
           tail = 0;
       }
   }
    synchronized public E get() throws InterruptedException {
        while (size == 0){
            this.wait();
        }
        E tmp = (E) array[head];
        head++;
        size--;
        this.notify();
        if(head == array.length){
            head = 0;
        }
        return tmp;
    }


}

public class ThreadDemo18 {
    public static void main(String[] args) throws InterruptedException {
        MyBlockingQueue<String> myBlockingQueue = new MyBlockingQueue(10);
        myBlockingQueue.put("10");
        myBlockingQueue.put("11");
        myBlockingQueue.put("12");
        myBlockingQueue.put("13");
        myBlockingQueue.put("14");
        System.out.println(myBlockingQueue.get());
        System.out.println(myBlockingQueue.get());
        System.out.println(myBlockingQueue.get());
        System.out.println(myBlockingQueue.get());
        System.out.println(myBlockingQueue.get());
        System.out.println(myBlockingQueue.get());
    }

}
